<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title.sync="title" width="940px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="125px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="配件编号" prop="modelNo">
            <el-input v-model="form.modelNo" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="modelName">
            <el-input v-model="form.modelName" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="型号" prop="modelModel">
            <el-input v-model="form.modelModel" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="序列号" prop="modelSerialno">
            <el-input v-model="form.modelSerialno" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="form.notes" class="input_text" type="textarea" :rows="4" style="width:760px" :readonly="isView" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-show="isView===false || isAdd===true" type="text" @click="cancel">取消</el-button>
      <el-button v-show="isAdd===false && isView===true" :loading="loading" type="primary" @click="cancel">返回</el-button>
      <el-button v-show="isView===false" :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addModel, editModel } from '@/api/tEquipment'
import { dictDetailAdd } from '@/api/dictDetail'
import { parseTimeShort } from '@/utils/index'
import { formatState } from '@/constants/consts'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    isView: {
      type: Boolean,
      required: true
    },
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      title: '',
      eqptId: '',
      form: {
        id: '',
        modelNo: '',
        modelName: '',
        modelModel: '',
        modelSerialno: '',
        state: '',
        notes: '',
        eqptId: ''
      },
      rules: {
        modelNo: [
          { required: true, message: '请输入配件编号', trigger: 'blur' }
        ],
        modelName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        modelSerialno: [{ required: true, message: '请输入序列号', trigger: 'blur' }],
        modelModel: [
          { required: true, message: '请输入型号', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const explorer = navigator.userAgent
    if (explorer.indexOf('Chrome') >= 0) {
      this.style = 'width: 200px'
    } else {
      this.style = 'width: 172px'
    }
  },
  mounted: function() {
    this.title = this.isAdd ? '新增' : (this.isView === true ? '详情' : '编辑')
  },
  methods: {
    parseTimeShort,
    onSubmitBrand(event) {
      const label = event.target.value
      if (label.length > 0) {
        let maxValue = 0
        let needadd = true
        for (var index in this.sparepartbrandlist) {
          const obj = this.sparepartbrandlist[index]
          console.log(obj)
          if (obj.label === label) {
            needadd = false
            break
          }
          if (parseInt(obj.value) > maxValue) {
            maxValue = parseInt(obj.value)
          }
        }
        if (needadd) {
          var data = { id: '', label: this.form.sparepartbrand, value: maxValue + 1, sort: 999, dict: { id: 18 }}
          dictDetailAdd(data)
            .then(res => {
              this.$notify({
                title: '添加成功',
                type: 'success',
                duration: 2500
              })
            })
            .catch(err => {
              this.loading = false
              console.log(err)
            })
        } // end of need add
      }
    },
    onSubmitModelname(event) {
      const label = event.target.value
      if (label.length > 0) {
        let maxValue = 0
        let needadd = true
        for (var index in this.sparepartmodellist) {
          const obj = this.sparepartmodellist[index]
          if (obj.label === label) {
            needadd = false
            break
          }
          if (parseInt(obj.value) > maxValue) {
            maxValue = parseInt(obj.value)
          }
        }
        if (needadd) {
          var data = {
            id: '',
            label: this.form.spareparttmodel,
            value: maxValue + 1,
            sort: 999,
            dict: { id: 17 }
          }
          dictDetailAdd(data)
            .then(res => {
              this.$notify({
                title: '添加成功',
                type: 'success',
                duration: 2500
              })
            })
            .catch(err => {
              this.loading = false
              console.log(err)
            })
        }
      }
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        }
      })
    },
    doAdd() {
      if (this.eqptId) {
        this.form['eqptId'] = this.eqptId
      }
      addModel(this.form)
        .then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.$parent.init()
        })
        .catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    doEdit() {
      editModel(this.form)
        .then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.supThis.init()
        })
        .catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        modelNo: '',
        modelName: '',
        modelModel: '',
        modelSerialno: '',
        state: '',
        notes: '',
        eqptId: ''
      }
    },
    // 返回状态值
    formateStateValue(row) {
      return formatState(row['state'])
    }
  }// endf of methods
}
</script>

<style scoped>
.input_text {
  width: 300px;
}
</style>
